﻿using UnityEngine;
using UnityEditor;

namespace HKTools
{
    public class HK_BuildInfoEditorWindow : EditorWindow
    {
        string customInfo = "Unknown";
        const string CUSTOM_INFO_KEY = "Unknown";

        [MenuItem("HKTools/HK_BuildInfoCustom（自定义构建信息）", false, HK_MenuPriority.HK_BuildInfoSystem)]
        public static void ShowWindow()
        {
            // 获取或创建窗口
            HK_BuildInfoEditorWindow window = GetWindow<HK_BuildInfoEditorWindow>(false, "Build Info Editor", true);
            window.minSize = new Vector2(400, 200);
        }

        void OnEnable()
        {
            // 从EditorPrefs加载保存的自定义信息
            customInfo = EditorPrefs.GetString(CUSTOM_INFO_KEY, "");
        }

        void OnGUI()
        {
            GUILayout.Space(10);
            EditorGUILayout.LabelField("Build Info Editor", EditorStyles.boldLabel);
            GUILayout.Space(10);

            // 自定义信息输入框
            EditorGUILayout.BeginVertical(EditorStyles.helpBox);
            GUILayout.Space(5);
            EditorGUILayout.LabelField("Custom Info:");
            customInfo = EditorGUILayout.TextArea(customInfo, GUILayout.Height(80));
            GUILayout.Space(5);
            EditorGUILayout.EndVertical();

            GUILayout.Space(10);
            EditorGUILayout.BeginHorizontal();
            GUILayout.FlexibleSpace();

            // 保存按钮
            if (GUILayout.Button("Save", GUILayout.Width(100), GUILayout.Height(30)))
            {
                SaveCustomInfo();
            }

            // 清除按钮
            if (GUILayout.Button("Clear", GUILayout.Width(100), GUILayout.Height(30)))
            {
                ClearCustomInfo();
            }
            EditorGUILayout.EndHorizontal();
            GUILayout.Space(10);

            // 提示信息
            EditorGUILayout.HelpBox("此自定义信息将在构建时自动添加到BuildInfo.json文件中。\n按F12键在运行时查看构建信息。", MessageType.Info);
        }

        void SaveCustomInfo()
        {
            // 保存到EditorPrefs
            EditorPrefs.SetString(CUSTOM_INFO_KEY, customInfo);
            Debug.Log("Custom build info saved successfully!");
        }

        void ClearCustomInfo()
        {
            customInfo = "";
            EditorPrefs.DeleteKey(CUSTOM_INFO_KEY);
            Debug.Log("Custom build info cleared!");
        }

        // 静态方法，供其他脚本调用以获取自定义信息
        public static string GetCustomBuildInfo()
        {
            return EditorPrefs.GetString(CUSTOM_INFO_KEY, "");
        }
    }
}